package com.example.travelplannernewstyle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Card extends ActionBarActivity {

	private Button buttonAddCard;
	private Button buttonCreditCard;
	private Button buttonBusCard;
	private ImageButton buttonHome;
	private Bundle extras;
	private boolean isCreditVisible;
	private boolean isBusVisible;
	private boolean boughtTicket;
	private TextView textCard1;
	private TextView textCard2;
	private TextView textViewHeader;
	private double amountOnCard;
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        /**Get extras*/
        extras = getIntent().getExtras();
        if(extras != null){
        	isCreditVisible = extras.getBoolean("isCreditVisible");
        	isBusVisible = extras.getBoolean("isBusVisible");
        	boughtTicket = extras.getBoolean("boughtTicket");
        	amountOnCard = extras.getDouble("currentAmount");
        }

        buttonAddCard = (Button) findViewById(R.id.buttonAddCard);
        buttonCreditCard = (Button) findViewById(R.id.buttonMyBankCard);
        buttonBusCard = (Button) findViewById(R.id.buttonMyBusCard);
        textCard1 = (TextView) findViewById(R.id.textViewCard1);
        textCard2 = (TextView) findViewById(R.id.TextViewCard2);
        
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        
        /**Set button visible/gone*/
        if(isCreditVisible){
        	buttonCreditCard.setVisibility(1);
        	textCard2.setVisibility(android.view.View.VISIBLE);
        }else{
        	buttonCreditCard.setVisibility(View.GONE);
        	textCard2.setVisibility(View.GONE); 
        }
        
        if(isBusVisible){
        	buttonBusCard.setVisibility(1);
        	textCard1.setVisibility(android.view.View.VISIBLE); 
        }else{
        	buttonBusCard.setVisibility(View.GONE);
        	textCard1.setVisibility(View.GONE); 
        }
        
        /**Set actionBar*/
        ActionBar actionBar = getSupportActionBar();
        actionBar.setIcon(null);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        View cView = getLayoutInflater().inflate(R.layout.header, null);
        actionBar.setCustomView(cView);
       
        buttonHome = (ImageButton) findViewById(R.id.imageHome);
        textViewHeader = (TextView) findViewById(R.id.textViewHeader);
        textViewHeader.setText("Kort");
        
        /**When button on top left is pressed, send to MainActivity*/
        buttonHome.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Card.this, MainActivity.class);
				addExtra(intent);
				finish();
			}
        });
        
        buttonBusCard.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Card.this, MyBusCard.class);
				addExtra(intent);
				finish();
			}
		});
        
        buttonCreditCard.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Card.this, MyCreditCard.class);
				addExtra(intent);
				finish();
			}
		});        
        
        buttonAddCard.setOnClickListener(new View.OnClickListener() {
 			@Override
 			public void onClick(View v) {
 				// TODO Auto-generated method stub
 				Intent intent = new Intent(Card.this, ChooseCard.class);
 				addExtra(intent);
 			}
 		});   
    }
    
    /**Add extra into the intent*/
    private void addExtra(Intent intent){
    	intent.putExtra("boughtTicket", boughtTicket);
		intent.putExtra("currentAmount", amountOnCard);
    	intent.putExtra("isCreditVisible", isCreditVisible);
    	intent.putExtra("isBusVisible", isBusVisible);
    	startActivity(intent);
    }
    
    @Override
    public void onBackPressed()
    {
    	Intent intent = new Intent(Card.this, MainActivity.class);
    	intent.putExtra("isCreditVisible", isCreditVisible);
    	intent.putExtra("isBusVisible", isBusVisible);
    	intent.putExtra("boughtTicket", boughtTicket);
		intent.putExtra("currentAmount", amountOnCard);
    	startActivity(intent);
    	finish();  
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}
